package com.vpclub.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.vpclub.bean.Teacher;
import com.vpclub.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * @author : leimingchao
 * @date Date : 2021年08月08日 2:29
 */
@Controller
public class TeacherController {

    @Autowired
    private TeacherService teacherService;

    @RequestMapping(value = "/getTeacher")
    public String getTeacher(@RequestParam(value = "id",required = false,defaultValue = "1") Integer id, Model model) {
        Teacher teacher = teacherService.getTeacher(1);
        model.addAttribute("teacher",teacher);
        return "success";
    }

    @RequestMapping(value = "/getTeachers")
    public String getTeachers(@RequestParam(value = "pageNumber",required = false,defaultValue = "1")Integer pageNumber, Model model) {
        //紧跟他的查询就是一个分页查询
        //起始页码从1开始
        PageHelper.startPage(pageNumber,5);
        List<Teacher> teachers = teacherService.getTeachers();
        //我们可以将查询的结果使用:将查询的结果放在pageInfo中，这个pageInfo就有非常多能够用的
        // PageInfo<Teacher> pageInfo = new PageInfo<>(teachers);
        //第二个传入连续要显示的页码
        PageInfo<Teacher> pageInfo = new PageInfo<>(teachers,6);
        System.out.println("当前页码：" + pageInfo.getPageNum());
        System.out.println("总页码：" + pageInfo.getPages());
        System.out.println("总记录数：" + pageInfo.getTotal());
        System.out.println("当前页有几条记录：" + pageInfo.getSize());
        System.out.println("当前页的pageSize：" + pageInfo.getPageSize());
        System.out.println("前一页：" + pageInfo.getPrePage());
        //查询结果
        System.out.println("结果：" + pageInfo.getList());
        int[] pageNumbers = pageInfo.getNavigatepageNums();
        model.addAttribute("pageInfo",pageInfo);
        return "success";
    }
}
